package com.mk.service.platform;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSON;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.domain.TMbOrderFlow;
import com.mk.domain.TOrder;
import com.mk.domain.TOrderLogistics;
import com.mk.enums.GlobalValues;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.apache.commons.collections.MapUtils;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 说明：订单表后台服务
 * 作者：292303959@qq.com 
 * 日期：2018-10-17 10:5:39 
 * 版本：1.0.0
 */
@Controller("tOrder")
@Api(group = "pl",title = "订单表服务", path = "/tOrder")
@SuppressWarnings("unused")
public class TOrderService extends BasePlatformService {
    @Inject
    private TOrder.Mapper tOrderMapper;
    @Inject
    private CpConfigService cpConfigService;
    @Inject
    private  TMbOrderFlow.Mapper tMbOrderFlowMapper;
    @Inject
    private TOrderLogistics.Mapper tOrderLogisticsMapper;


    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存订单表", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tOrder", title = "订单表", type = TOrder.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull TOrder tOrder) {
        String id = S2.UUID();
        tOrder.setOrderid(id);
        tOrderMapper.insertTemplate(tOrder);
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存订单表", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "tOrder", title = "订单表", type = TOrder.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull TOrder tOrder) {
        return tOrderMapper.updateTemplateById(tOrder);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定订单表", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "订单表的主键", type = String.class)
    @ApiReturn(description = "订单表", type = TOrder.class)
    public TOrder view(@NotNull String id){
        return tOrderMapper.single(id);
    }

    @With(ILogin.class)
    @PostAction("unlinePay.json")
    @ApiMethod(title = "线下支付", path = "/unlinePay.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "orderid", title = "订单ID", type = String.class)
    @ApiParam(name = "mid", title = "会员ID", type = String.class)
    @ApiParam(name = "orderamount", title = "会员ID", type = BigDecimal.class)
    @ApiReturn(description = "更新的数量", type = Integer.class)
    public int unlinePay(String orderid,String mid,String orderamount) {
        BigDecimal fscale=BigDecimal.ZERO;
        BigDecimal rscale=BigDecimal.ZERO;
        String isfirst="0";
        //查询是否首单
        Integer ocount=tOrderMapper.selectIsFirst(mid);
        if(0<ocount){
            fscale=new BigDecimal(cpConfigService.getValue(GlobalValues.CP_CONFIG_FSCALE.value()));
            isfirst="1";
        }else{
            //查询是否有返单奖励
            int mtype=tOrderMapper.selectMebType(mid);
            if(mtype>0){
                rscale=new BigDecimal(cpConfigService.getValue(GlobalValues.CP_CONFIG_RSCALE.value()));
            }
            isfirst="0";
        }
        tOrderMapper.updateOrderPay(orderid,GlobalValues.Z_PAYTYPE_2.value(),fscale,rscale,isfirst);
        //记录支付流水
        TMbOrderFlow tMbOrderFlow = new TMbOrderFlow();
        tMbOrderFlow.setOfid(S2.UUID());
        tMbOrderFlow.setMid(mid);
        tMbOrderFlow.setOrderid(orderid);
        tMbOrderFlow.setPayamount(new BigDecimal(orderamount));
        tMbOrderFlow.setPaytype(GlobalValues.Z_PAYTYPE_2.value());
        tMbOrderFlow.setOfdate(DateUtil.now());
        tMbOrderFlow.setOfflag(GlobalValues.Z_FLOW_OFFLAG_0.value());
        tMbOrderFlowMapper.insert(tMbOrderFlow);
        return 0;
    }
    @With(ILogin.class)
    @PostAction("sendOrder.json")
    @ApiMethod(title = "查询指定订单表", path = "/sendOrder.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "id", title = "订单表的主键", type = String.class)
    @ApiReturn(description = "订单表", type = TOrder.class)
    public int sendOrder(TOrderLogistics tOrderLogistics){
        tOrderLogistics.setOlid(S2.UUID());
        Map loginfo= com.alibaba.fastjson.JSON.parseObject(tOrderLogistics.getLogisticsname());
        tOrderLogistics.setLogisticsname(MapUtils.getString(loginfo,"lcvalue"));
        tOrderLogistics.setLogisticscode(MapUtils.getString(loginfo,"lccode"));
        tOrderLogisticsMapper.insert(tOrderLogistics);
        //更新订单状态和明细状态
        return tOrderMapper.updateSendOrder(tOrderLogistics.getOrderid());
    }



}
